#!/usr/local/python/bin/python

import os
import subprocess
import fnmatch
import glob

def add_directory(directory):
    remove_cmd = ["synoindex", "-A", directory]
    p = subprocess.Popen(remove_cmd, stdout=subprocess.PIPE)
    print "added directory ["+directory+"]"
    
def remove_directory(directory):
    remove_cmd = ["synoindex", "-D", directory]
    p = subprocess.Popen(remove_cmd, stdout=subprocess.PIPE)
    print "removed directory ["+directory+"]"
    
def remove_entry(file_path):
    remove_cmd = ["synoindex", "-d", file_path]
    p = subprocess.Popen(remove_cmd, stdout=subprocess.PIPE)
    print "removed file ["+file_path+"]"

def add_entry(file_path):
    remove_cmd = ["synoindex", "-a", file_path]
    p = subprocess.Popen(remove_cmd, stdout=subprocess.PIPE)

def get_directories_entries():
    matches = []
    cmd = ["/usr/syno/pgsql/bin/psql", "mediaserver", "admin", "-tA", "-c", "select path from directory;"]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out, err = p.communicate()
    for file_name in filter(None, out.split('\n')):
        matches.append(file_name)
    return set(matches)

def get_media_entries():
    matches = []
    cmd = ["/usr/syno/pgsql/bin/psql", "mediaserver", "admin", "-tA", "-c", "select path from video;"]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out, err = p.communicate()
    for file_name in filter(None, out.split('\n')):
        matches.append(file_name)
    return set(matches)

def clean_index():
    cmd = ["/usr/syno/pgsql/bin/psql", "mediaserver", "admin", "-tA", "-c", "select path from video;"]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out, err = p.communicate()
    for file_name in filter(None, out.split('\n')):
        if not os.path.exists(file_name):
            remove_file(file_name)

video_files_ext = ('.avi', '.mp4', '.mkv')
def get_media_files(root_path):
    matches = []
    for root, dirnames, filenames in os.walk(root_path):
      for filename in filenames:
          if filename.endswith(video_files_ext):
              matches.append(os.path.join(root, filename))
    return set(matches)

def get_local_directories(files_path):
    directories = []
    for file_path in files_path:
        directories.append(os.path.dirname(file_path))
    return set(directories)

media_local_files = get_media_files("/volume1/video")
media_db_entries = get_media_entries()
present_files = media_local_files.intersection(media_db_entries)
medias_to_delete = media_db_entries.difference(present_files)
for media in medias_to_delete:
    remove_entry(media)

medias_to_add = media_local_files.difference(present_files)
for media in medias_to_add:
    add_entry(media)


media_directories_entries = get_directories_entries()
for directory in media_directories_entries:
    if not os.path.exists(directory):
        remove_directory(directory)

file_directories = get_local_directories(present_files)

present_directories = media_directories_entries.intersection(file_directories)

for directory in file_directories.difference(present_directories):
    add_directory(directory)
